package com.luoxudong.phonenumber.service.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Title: DateUtil
 * Description: 时间日期工具类
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 *
 * @author 罗旭东
 * @CreateDate 2013-5-28 下午3:52:11
 * @version 1.0
 */
public class DateUtil {
	/**
	 * 格式 yyyy-MM-dd HH:mm:ss
	 */
	public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 格式 yyyyMMddHHmmss
	 */
	public static final String DATE_FORMAT_1 = "yyyyMMddHHmmss";
	
	/**
	 * 格式 yyyy-MM-dd HH:mm
	 */
	public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm";
	
	/**
	 * 格式 yyyy-MM-dd
	 */
	public static final String DATE_FORMAT_3 = "yyyy-MM-dd";
	
	/**
	 * 格式 yyyy年MM月dd日
	 */
	public static final String DATE_FORMAT_4 = "yyyy年MM月dd日";
	
	/**
	 * 格式 HH:mm:ss
	 */
	public static final String DATE_FORMAT_5 = "HH:mm:ss";
	
	public static final String DATE_FORMAT_6 = "yyyyMMdd.hhmmss";
	
	public static final String DATE_FORMAT_7 = "yyyy/MM/dd HH:mm";
	
	public static final String DATE_FORMAT_8 = "yyyy/MM/dd HH:mm:ss";
	
	/**
	 * 常用的格式化对象
	 */
	public static final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DEFAULT, Locale.getDefault());
	
	/**
	 * 年月日时分秒
	 */
	public static final SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT_1, Locale.getDefault());
	
	/**
	 * 
	 */
	public static final SimpleDateFormat sdf5 = new SimpleDateFormat(DATE_FORMAT_2, Locale.getDefault());
	
	/**
	 * 年-月-日
	 */
	public static final SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT_3, Locale.getDefault());
	
	/**
	 * 时:分:秒
	 */
	public static final SimpleDateFormat sdf3 = new SimpleDateFormat(DATE_FORMAT_5, Locale.getDefault());
	
	/**
	 * 
	 */
	public static final SimpleDateFormat sdf4 = new SimpleDateFormat(DATE_FORMAT_6, Locale.getDefault());
	
	/**
	 * 时间差
	 */
	private static long span = 0;

	/**
	 * 
	 * getDate 获取当前时间
	 * 
	 */
	public static Date getDate()
	{
		Date nowAndroid = new Date();
		long nowPlant = nowAndroid.getTime() + span;

		Date nowPlantDate = new Date(nowPlant);

		return nowPlantDate;
	}
	
	/**
	 * 
	 * getSystemCalendar 获取当前Calendar
	 */
	public static Calendar getSystemCalendar()
	{
		Calendar cal = Calendar.getInstance();
		cal.setTime(getDate());
		return cal;
	}
	
	/**
	 * 
	 * getAndroidDate 获取本地时间
	 * 
	 */
	public static Date getAndroidDate(Date plantDate)
	{
		long nowAndroid = plantDate.getTime() - span;
		Date androidDate = new Date(nowAndroid);
		return androidDate;
	}

	/**
	 * 
	 * getDateString 获取字符串形式的当前时间
	 * 
	 */
	public static String getSystemDateTimeString()
	{
		Date date = getDate();
		return sdf.format(date);
	}

	/**
	 * 
	 * getDateString 获取日期字符串
	 * 
	 */
	public static String getDateString(Date date)
	{
		return sdf.format(date);
	}
	
	public static String getThisDateToString(String format)
	{
		Date date = getDate();
		
		if (format == null)
		{
			format = DATE_FORMAT_DEFAULT;
		}
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);

		return dateFormat.format(date);
	}

	/**
	 * 
	 * getDateToString 根据指定日期格式获取对应字符串
	 * 
	 */
	public static String getDateToString(Date date, String format)
	{
		if (date == null)
		{
			return null;
		}
		
		if (format == null)
		{
			format = DATE_FORMAT_DEFAULT;
		}
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);

		return dateFormat.format(date);

	}
	
	public static Date getDefaultDateByParse(String dateTime)
	{
		Date date = null;
		
		try{
			date = sdf.parse(dateTime);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 根据字符串获取date对象
	 * @param dateTime
	 * @param format
	 * @return
	 * @return Date
	 */
	public static Date getDateTimeFromString(String dateTime, String format)
	{
		Date date = null;
		
		try
		{
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			date = dateFormat.parse(dateTime);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		
		return date;
	}
	
	public static long getSpan()
	{
		return span;
	}

	public static void setSpan(long span)
	{
		DateUtil.span = span;
	}
	
	/**
	 * 
	 * getDayOfWeek(这里用一句话描述这个方法的作用)
	 * 计算今天是星期几
	 */
	public static int getDayOfWeek()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate());
		return calendar.get(Calendar.DAY_OF_WEEK);
	}
	
	/**
	 * 
	 * getBeginAndEndDateThisWeek(这里用一句话描述这个方法的作用)
	 * 获取本周的起始截止日期
	 */
	public static List<Date> getBeginAndEndDateThisWeek()
	{
		List<Date> ret = new ArrayList<Date>();
		Calendar calendar = Calendar.getInstance();
		int weekday = calendar.get(7) - 2;
		calendar.add(5, -weekday);
		ret.add(calendar.getTime());
		calendar.add(5, 6);
		ret.add(calendar.getTime());
		return ret;
	}
	
	/**
	 * 获取相对于今天的日期
	 * @param rel
	 * @return
	 */
	public static Date getRelativeDate(int num)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getDate());
		calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) + num);  
		return calendar.getTime();
	}
	
	/**
	 * 获取相对于指定日期的天数
	 * @param relDate
	 * @param num
	 * @return
	 */
	public static Date getRelativeDate(Date relDate, int num)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(relDate);
		calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) + num);  
		return calendar.getTime();
	}
}
